
var APP = function () {
    return {
        serverIP: function () {
            return window.localStorage.getItem('serverIP');
        },

        serverIP1: '192.168.1.22:9003',
        // serverIP1: '192.168.1.69:80',

        //serverIP2: '43w18n2519.yicp.fun:443',
        serverIP2: '122.227.132.102:9001',
        // serverIP2: '192.168.1.69:80',



        ajax: function (cmd, parm, successCallback, errorCallback) {

            if (api.connectionType == 'none') {
                vm.showLoading = false;
                vm.refreshLoading = false;
                vm.ifConfirm = false;
                vm.scanDisabled = false;

                if (typeof (layer) != 'undefined') {
                    layer.open({
                        content: '当前网络不可用,请连接网络!',
                        skin: 'msg',
                        time: 2
                    });
                }
                return false;
            }
            var url = APP.serverIP() + '/Frame/SqlData?cmdname=' + cmd + '&platform=' + api.systemType + '【' + api.deviceId + '】';
            // console.log('url', JSON.stringify(url))
            if (APP.getUserInfo()) {
                if (!parm.hasOwnProperty("P_UserID")) {
                    parm.P_UserID = APP.getUserInfo().UserID;
                    parm.P_UserName = APP.getUserInfo().UserName;
                    parm.P_UserCode = APP.getUserInfo().UserCode;
                }
            }
            api.ajax({
                headers: {
                    'Version': api.appVersion,
                    'PackageName': 'com.wote.app'
                },
                url: url,
                data: {
                    values: parm
                },
                method: 'post',
                dataType: 'json'
            }, function (ret, err) {
                if (ret) {
                    if (ret.status == 200) {
                        if (successCallback) successCallback(ret.data);
                    } else {
                        if (vm) {
                            vm.showLoading = false;
                            vm.refreshLoading = false;
                            vm.ifConfirm = false;
                            vm.scanDisabled = false;
                            alert(cmd + '\n' + APP.serverIP() + '\n' + '错误代码1:' + '\n' + JSON.stringify(ret));
                        }
                    }
                } else {
                    if (errorCallback) {
                        errorCallback(err);
                    } else {
                        if (vm) {
                            vm.showLoading = false;
                            vm.refreshLoading = false;
                            vm.ifConfirm = false;
                            vm.scanDisabled = false;
                            alert(cmd + '\n' + APP.serverIP() + '\n' + '错误代码2:' + '\n' + JSON.stringify(err));
                        }

                        pingIP(APP.serverIP1);
                        pingIP(APP.serverIP2);
                    }


                }
            });
        },
        ajax2: function (url, parm, successCallback, errorCallback) {
            if (api.connectionType == 'none') {
                if (typeof (layer) != 'undefined') {
                    layer.open({
                        content: '当前网络不可用,请连接网络!',
                        skin: 'msg',
                        time: 2
                    });
                }
                return false;
            }
            var url = APP.serverIP() + url;

            if (APP.getUserInfo()) {
                if (!parm.hasOwnProperty("P_UserID")) {
                    parm.P_UserID = APP.getUserInfo().UserID;
                    parm.P_UserName = APP.getUserInfo().UserName;
                    parm.P_UserCode = APP.getUserInfo().UserCode;
                }
            }
            api.ajax({
                headers: {
                    'Version': api.appVersion,
                    'PackageName': 'com.lls.app'
                },
                url: url,
                data: {
                    values: parm
                },
                method: 'post',
                dataType: 'json'
            }, function (ret, err) {
                if (ret) {
                    if (ret.status == 200) {
                        if (successCallback) successCallback(ret.data);
                    } else {
                        if (vm) {
                            alert(url + '\n' + APP.serverIP() + '\n' + '错误代码1:' + '\n' + JSON.stringify(ret));
                        }
                    }
                } else {
                    if (errorCallback) {
                        errorCallback(err);
                    } else {
                        if (vm) {
                            alert(url + '\n' + APP.serverIP() + '\n' + '错误代码2:' + '\n' + JSON.stringify(err));
                        }

                        pingIP(APP.serverIP1);
                        pingIP(APP.serverIP2);
                    }
                }
            });
        },
        jqueryAjax: function (url, parm, successCallback, errorCallback) {
            if (api.connectionType == 'none') {
                if (typeof (layer) != 'undefined') {
                    layer.open({
                        content: '当前网络不可用,请连接网络!',
                        skin: 'msg',
                        time: 2
                    });
                }
                return false;
            }
            var url = APP.serverIP() + url;

            if (APP.getUserInfo()) {
                if (!parm.hasOwnProperty("P_UserID")) {
                    parm.P_UserID = APP.getUserInfo().UserID;
                    parm.P_UserName = APP.getUserInfo().UserName;
                    parm.P_UserCode = APP.getUserInfo().UserCode;
                }
            }

            $.ajax({
                url: url,
                async: true,
                data: parm,
                type: "post",
                dataType: 'json',
                success: function (ret) {
                    // alert(JSON.stringify(ret));
                    if (ret) {
                        console.log('ret', JSON.stringify(ret))
                        if (ret) {
                            if (successCallback) successCallback(ret);
                        } else {
                            if (vm) {
                                alert(url + '\n' + APP.serverIP() + '\n' + '错误代码1:' + '\n' + JSON.stringify(ret));
                            }
                        }
                    } else {
                        console.log('err', JSON.stringify(err))
                        if (errorCallback) {
                            errorCallback(err);
                        } else {
                            if (vm) {
                                alert(url + '\n' + APP.serverIP() + '\n' + '错误代码2:' + '\n' + JSON.stringify(err));
                            }

                            pingIP(APP.serverIP1);
                            pingIP(APP.serverIP2);
                        }
                    }
                }
            });
        },
        getUserInfo: function () {
            if (window.localStorage.getItem('userInfo')) {
                return JSON.parse(window.localStorage.getItem('userInfo'))
            } else {
                return null;
            }
        },
        uploadFile: function (folder, fileSrc, callback) {
            console.log('folder', JSON.stringify(folder))
            console.log('fileSrc', JSON.stringify(fileSrc))
            console.log('api.appVersion', JSON.stringify(api.appVersion))
            var url = APP.serverIP() + '/Frame/uploadFile?dirName=' + folder;
            api.ajax({
                headers: {
                    'Version': api.appVersion,
                    'PackageName': 'com.lls.app'
                },
                url: url,
                method: 'post',
                data: {
                    files: {
                        file: fileSrc
                    }
                }
            }, function (ret, err) {
                console.log(JSON.stringify(ret))
                if (ret) {
                    if (ret.status && ret.status == 200) {
                        if (callback) callback(ret.data);
                    } else {
                        if (vm) {
                            vm.showLoading = false;
                            vm.refreshLoading = false;
                            vm.$toast('上传失败,请稍后再试');
                        }

                    }
                } else {
                    if (vm) {
                        vm.showLoading = false;
                        vm.refreshLoading = false;
                        vm.$toast('程序好像出错了,请稍后再试\n' + 'errcode:' + err.code);
                    }
                }
            });
        },

        exitApp: function () {
            setTimeout(function () {
                api.addEventListener({
                    name: 'keyback'
                }, function (ret, err) {
                    api.toast({
                        msg: '再按一次返回键退出' + api.appName,
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.addEventListener({
                        name: 'keyback'
                    }, function (ret, err) {
                        api.closeWidget({
                            id: api.appId,
                            retData: {
                                name: 'closeWidget'
                            },
                            silent: true
                        });
                    });
                    setTimeout(function () {
                        APP.exitApp();
                    }, 2000)
                });
            }, 400);

        },

        getFormatDate: function (date) {
            if (!date) date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            if (hour >= 0 && hour <= 9) {
                hour = "0" + hour;
            }
            if (minute >= 0 && minute <= 9) {
                minute = "0" + minute;
            }
            if (second >= 0 && second <= 9) {
                second = "0" + second;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + hour + seperator2 + minute
                + seperator2 + second;
            return currentdate;
        },

        getFormatDateTime: function (date) {
            if (!date) date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            if (hour >= 0 && hour <= 9) {
                hour = "0" + hour;
            }
            if (minute >= 0 && minute <= 9) {
                minute = "0" + minute;
            }
            if (second >= 0 && second <= 9) {
                second = "0" + second;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + hour + seperator2 + minute;
            return currentdate;
        },
        pingIP(ip) {
            api.ajax({
                url: ip + '/Frame/SqlData?cmdname=ConnectionTest',
                data: {
                    values: {}
                },
                timeout: 20,
                method: 'post',
                dataType: 'json'
            }, function (ret, err) {
                if (ret) {
                    window.localStorage.setItem('serverIP', ip);
                } else {

                }
            });
        },
        getFormatDate2(fmt, date) {
            if (!fmt) fmt = 'yyyy-MM-dd hh:mm:ss';
            if (!date) date = new Date();
            var o = {
                "M+": date.getMonth() + 1,     //月份
                "d+": date.getDate(),     //日
                "h+": date.getHours(),     //小时
                "m+": date.getMinutes(),     //分
                "s+": date.getSeconds(),     //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds()    //毫秒
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            }

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                }
            }


            return fmt;
        }

    }
}()
if (Vue) {
    Vue.component('van-nav-bar-main', {
        props: ['title', 'left-text', 'right-text', 'left-arrow'],
        data: function () {
            return {
                safeHeight: 0
            }
        },
        props: {
            title: {
                type: String,
                default: ''
            },
            leftText: {
                type: String,
                default: ''
            },
            rightText: {
                type: String,
                default: ''
            },
            leftArrow: {
                type: Boolean,
                default: false
            }
        },
        template: '<div>\
                    <van-nav-bar :style="{\'padding-top\':safeHeight + \'px\'}" :title="title" :left-text="leftText" :right-text="rightText" :left-arrow="leftArrow" @click-left="clickLeft" @click-right="clickRight">\
                    <slot slot="title" name="title" style="color:#000"></slot>\
                    <slot slot="left" name="left"></slot>\
                    <slot slot="right" name="right"></slot>\
                    </van-nav-bar>\
                    </div>',
        created: function () {
            this.getSafeHeight(0);
        },
        methods: {
            getSafeHeight: function (index) {
                var that = this;
                if (typeof (api) !== 'undefined') {
                    that.safeHeight = api.safeArea.top;
                } else {
                    if (index >= 10) {
                        this.safeHeight = 0;
                        return;
                    }
                    index++;
                    setTimeout(function () {
                        that.getSafeHeight(index);
                    }, 100)
                }

            },
            clickLeft: function () {
                this.$emit('click-left');
            },
            clickRight: function () {
                this.$emit('click-right');
            }
        }
    })
}

// Vue.component('serve-img1', {
//     props: ['src'],
//     template: '<img :src="returnServeImg(src)" />',
//     methods: {
//         returnServeImg(src) {
//             if (src && src != 'null') {
//                 return 'http://' + APP.serverIP + src;
//             } else {
//                 return '../../image/header.png';
//             }

//         }
//     }
// })
// Vue.component('serve-img2', {
//     props: ['src'],
//     template: '<img :src="returnServeImg(src)" />',
//     methods: {
//         returnServeImg(src) {
//             if (src) {
//                 return 'http://' + APP.serverIP + src;
//             } else {
//                 return '../../image/no.png';
//             }

//         }
//     }
// })


$(function () {

    // vant下拉刷新与向上滚动的冲突问题解决
    setTimeout(function () {
        if (typeof (vm) != 'undefined') {
            if (vm.loadingDisabled != undefined) {
                $('.content-refresh').scroll(function () {
                    if ($(this).scrollTop() == 0) {
                        vm.loadingDisabled = false;
                    } else {
                        vm.loadingDisabled = true;
                    }
                })
            }
        }
    }, 200)
    //vue移动端点击方法300毫秒延迟问题解决
    if (typeof (FastClick) !== 'undefined') {
        FastClick.attach(document.body)
    }

})
